package jms.connection;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQPrefetchPolicy;

public class JmsConnection {
	
	private ConnectionFactory connectionFactory;
	private static JmsConnection jmsConnection;
	
	private  JmsConnection(){
		ActiveMQConnectionFactory aConnectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
		ActiveMQPrefetchPolicy p = new ActiveMQPrefetchPolicy();
		p.setQueuePrefetch(1);
		aConnectionFactory.setPrefetchPolicy(p);
		connectionFactory = aConnectionFactory;
	}
	
	public static JmsConnection getInstance(){
		if (jmsConnection==null) {
			synchronized (JmsConnection.class) {
				if (jmsConnection==null) {
					jmsConnection = new JmsConnection();
				}
			}
		}
		return jmsConnection;
	}
	
	public Connection createConnection(){
		try {
			return connectionFactory.createConnection();
		} catch (JMSException e) {
			e.printStackTrace();
		}
		return null;
	}
	

	
	

}
